/**
 * 食药公用js
 * 
 * */
define([
    'base',
    'bootstrapDialog',
    'bootstrapTable',
    'highcharts',
    'exporting',
    'highchartsCN',
    'sandSignika',
    'jqViewer',
    'jqPrint',
    'plugins/fileInput/fileinput.min',
    'css!global/plugins/fileInput/fileinput.min.css',
    "css!global/plugins/viewer/viewer.min.css"
], function (base, BootstrapDialog,highcharts,jqPrint) {
    //默认加载基础库
    base();
    return {
        list: function () {
            var myTable = $('#dataList');
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url:'list.json'})
            };
            //table按需刷新动作
            var doAction = function (obj) {
                myTable.bootstrapTable('refresh', {url: 'list.json?pid=' + obj.id})
            };

            //table当前行的响应事件
            window.operateEvents = {
                'click .update': function (e, value, row) {
                	   winModal({ 
                		   row: row,
                		   title:'办件事项类型',
                		   btName:'保存'
                	   });
                },
                'click .status': function (e, value, row) {
                    $.baseUtils.oneStatus({
                        row: row,
                        tableName: myTable,
                        nameText: '事项分类',
                        nameValue: row.title,
                        url: '../status/action'
                    });
                },
                'click .delete': function (e, value, row) {
                    $.baseUtils.oneDelete({
                        row: row,
                        tableName: myTable,
                        nameText: '办件事项类型',
                        nameValue: row.title,
                        url: '../delete/action'
                    });
                }
            };
            
            $(".toolbar-save").on('click', function () {
            	 winModal({ 
          		   title:'办件事项类型',
          		   btName:'保存'
          	   });
            });

            
            //table表格
            myTable.bootstrapTable({
                url: 'list.json', // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [
                    {
                        field: 'id',
                        title: '事项类型编号',
                        align: 'center',
                        width: '5%'
                    }, {
                        field: 'title',
                        title: '事项类型名称',
                        width: '20%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-hashtag text-primary" aria-hidden="true"></i>&emsp;<b>' + value
                        }
                    },
                    {
                        field: 'icon',
                        title: '事项图标',
                        width: '20%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + value 
                        }
                    }, {
                    	field: 'description',
                        title: '事项类型描述',
                        width: '20%',
                        formatter: function (value, row, index) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + value 
                        }
                    }, {
                    	field: 'sort',
                        title: '排序',
                        align: 'center',
                        width: '5%',
                        formatter: function(value, row, index){
                        	  return '<b>' + value
                        }
                    }, {
                        field: 'status',
                        title: '状态',
                        width: '10%',
                        align: 'center',
                        formatter: function (value, row, index) {
                        	 var result ="";
                        	  switch (value) {
                              case 0:
                                  result = "<span class=\"label label-danger\"><i class=\"fa fa-lock\" aria-hidden=\"false\"></i>&nbsp;禁用</span";
                                  break;
                              case 1:
                                  result = "<span class=\"label label-primary\"><i class=\"fa fa-unlock\" aria-hidden=\"true\"></i>&nbsp;启用</span";
                                  break;
                              default:
                                  break;
                          }
                            return result;
                        }
                    },{
                        field: 'action',
                        title: '操作',
                        align: 'center',
                        width: '10%',
                        formatter: operateFormatter,
                        events: operateEvents
                    }
                ]
            });

            //搜索刷新
            $('#search-btn').on('click', function () {
                tableRefresh();
            });

  
            //刷新
            $(".toolbar-refresh").on('click', function () {
                tableRefresh();
            });

            //批量修改状态
            $(".toolbar-batchStatus").on('click', function () {
                var entityList = myTable.bootstrapTable('getSelections');
                var row = [], names = [];
                $.each(entityList, function (i, v) {
                    names.push(v.itemName);
                    row.push(v.id);
                });
                if (row.length > 0) {
                    $.baseUtils.batchAction({
                        row: row,
                        tableName: myTable,
                        nameText: '食药申请',
                        nameValue: names,
                        url: '../status/batchAction'
                    });
                } else {
                    msgTool.error("请选择虽少一条记录后再操作", "信息提示");
                }

            });


            //搜索参数传递
            function queryParams(params) {
                params.search = $("#search").val();
                return {
                    limit: params.limit,
                    offset: params.offset,
                    search: params.search
                };
            }

            //回显
            function responseHandler(res) {
                return res.rows;
            }

            //模态框
            function winModal(obj) {
                BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_DEFAULT,
                    tabindex: null,//解决select2搜索框失去焦点的问题
                    title: obj.title,
                    closable: true,	//关闭箭头启动
                    closeByBackdrop: false,	//点击空白不消失
                    closeByKeyboard: true,//ESC取消禁用
                    message: $('<div></div>').load('../save/form.html'),
                    data: { 
                        'row': obj.row
                    },
                    buttons: [{
                        label: '取消',
                        cssClass: 'btn-white cancel',
                        icon: 'fa fa-times',
                        action: function (dialog) {
                            dialog.close();
                        }
                    }, {
                        label: obj.btName,
                        //hotkey: 13,//回车提交
                        cssClass: 'btn-primary',
                        icon: 'fa fa-check',
                        action: function (dialog) {
                            //返回验证结果
                            var flag = false;
                            flag = $("#winModal").validate().form();
                            if (flag) {
                                $.baseUtils.ajax({
                                    dialog: dialog,
                                    url: '../save/action',
                                    data: $("#winModal").serialize(),
                                    text: "保存",
                                    isTable: true,
                                    tableName: '#dataList',
                                    isDialog: true
                                })
                            }
                        }
                    }],
                    onshown: function (dialog) {
                    	var row=obj.row;
                        if (row != null) {
                            $('#id').val(row.id);
                            $('#title').val(row.title);
                            $('#icon').val(row.icon);
                 		    $("#pictureImg").attr("src",row.picture);
                            $('#pictureVal').val(row.picture);
                            $('#description').val(row.description);
                            $('#sort').val(row.sort);
                        }
                    }
                });
            }
        },
        view: function () {


        },

        save:function () {
        	  $(function () {
        	        var e = "<i class='fa fa-times-circle'></i>";
        	        $("#winModal").validate({
        	            rules: {
        	            	title: {
        	                    required: true,
        	                    rangelength: [2, 30]
        	                 },
        	             	icon: {
        	                    required: true,
        	                    rangelength: [2, 30]
        	                 },
        	                 description: {
        	                    required: true,
        	                    rangelength: [2, 30]
        	                 },
        	             	sort: {
        	                    required: true,
        	                    number:true
        	                 }
        	              
        	            },
        	            messages: { //自定义验证消息
        	                title: {
        	                    required: e + "请输入事项分类名称",
        	                    rangelength: $.validator.format(e + "事项分类名称位数要在{0}-{1}个字符之间。")
        	                },
        	                icon: {
        	                    required: e + "请输入图标",
        	                    rangelength: $.validator.format(e + "图标位数要在{0}-{1}个字符之间。")
        	                },
        	                description: {
        	                    required: e + "请输入详情描述"
        	                },
        	                sort: {
        	                    required: e + "请输入序号"
        	                }
        	            }
        	        });
        	    });
        	    
        	 
        	    

        }
    }
});






